package com.ebupt.migu.music.commons.selfdom.demo.impl;

import com.alibaba.fastjson.JSONArray;
import com.ebupt.migu.globalpopup.entity.ConditionRelation;
import com.ebupt.migu.globalpopup.enums.ConditionEnum;
import com.ebupt.migu.music.commons.selfdom.demo.ExType;
import com.ebupt.migu.music.commons.selfdom.demo.impl.identifier.AbstractIdentifierExImpl;
import com.ebupt.migu.music.commons.selfdom.demo.impl.identifier.AbstractIdentifierImpl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 达人身份（type)
 *
 * @author wangzhu
 * @since 2021-12-31
 */
public class ExTypeImpl extends AbstractIdentifierExImpl implements ExType {


    /**
     * type
     */
    private boolean cdType(List data, ConditionRelation conditionRelation,Integer conditionKey) {
        return super.getIdentifier(data, conditionRelation.getConditionData(), conditionRelation.getRelation(),conditionKey);
    }

    /**
     * 更具不同的子条件判断返回判断结果（子条件第一版只支持or）
     */
    @Override
    public boolean getExType(Map data, List<ConditionRelation> conditionRelations) {
        LinkedHashMap<Boolean, Integer> booleanIntegerLinkedHashMap = new LinkedHashMap<>();
        // 排序
        super.sort(conditionRelations);
        for (ConditionRelation conditionRelation : conditionRelations) {
            boolean expert = true;
            Object o = data.get(ConditionEnum.getKeyValue(conditionRelation.getPropertyCode(), conditionRelation.getConditionKey()));
            if (o==null||o.equals("")) {
                expert=false;
                booleanIntegerLinkedHashMap.put(expert,conditionRelation.getCConnector());
                break;
            }
            List<Map> maps = JSONArray.parseArray( o.toString(), Map.class);
            List<String> types = maps.stream().map(map -> {
                return map.get("type").toString();
            }).collect(Collectors.toList());
             List value =types;
             switch (conditionRelation.getConditionKey()) {
                case 1:
                    expert = this.cdType(value, conditionRelation,conditionRelation.getConditionKey());
                    break;
                default:
                    break;
            }
            booleanIntegerLinkedHashMap.put(expert, conditionRelation.getCConnector());
        }
        return super.getRelation(booleanIntegerLinkedHashMap);
    }


}
